<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
//use App\Http\Models\CarModel;
use Illuminate\Http\Request;
use App\Model\CarModel;

class ModelController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    /**
     * 获取brands
     */
    public function getList( Request $request ){

        $res = [];
        $get = $request->all();
        $page = isset( $get['page'] )? intval($get['page']) : 1;
        $number = isset( $get['number'] )? intval($get['number']) : 20;
        $where = [];

        if( !empty( $get['brand_name'] ) ){
            $where[] =[ 'car_brand.name', 'like', '%' . $get['brand_name'] .'%'];
        }

        if( !empty( $get['factory_name'] ) ){
            $where[] =[ 'car_factory.name', 'like', '%' . $get['factory_name'] .'%'];
        }

        if( !empty( $get['factory_id'] ) ){
            $where[] =[ 'car_factory.id', '=', $get['factory_id'] ];
        }

        if( !empty( $get['name'] ) ){
            $where[] =[ 'car_model.name', 'like', '%' . $get['name'] .'%'];
        }
        $list = CarModel::where( $where )
            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->forPage($page, $number)
            ->select(
                'car_brand.id as brand_id'
                , 'car_brand.name as brand_name'
                , 'car_factory.id as factory_id'
                , 'car_factory.name as factory_name'
                , 'car_model.id'
                , 'car_model.name'
                )
        ->get();

        $count = CarModel::where( $where )
            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->count();

        $res = [
            'status'=>0,
            'total'=> $count,
            'page_total'=> ceil( $count / $number),
            'list'=> $list
        ];
        echo json_encode( $res );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function single( $id ){

        $model = CarModel::where('car_model.id',$id)
            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->select(
                'car_model.id',
                'car_model.name',
                'car_factory.id as factory_id',
                'car_factory.name as factory_name',
                'car_brand.id as brand_id',
                'car_brand.name as brand_name'
            )
            ->first();
//        $single = !empty( $model ) ? $model[0] : '';
        $single = !empty( $model ) ? $model : [];
        echo json_encode( $single );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function edit( Request $request, $model_id ){

        $post = $request->all();
        $params = $post;
        if( !empty( $model_id ) ){
            $res = CarModel::edit($model_id, $params);
        }else{
            $car_model = new CarModel();
            $car_model->factory_id =$post['factory_id'];
            $car_model->name =$post['name'];
            $res = $car_model->save();
            
        }

        echo json_encode( $res );
    }

}
